package com.CuiChangHe.studentmanager.service.userinfo.impl;

import com.CuiChangHe.studentmanager.dao.UserInfoDao;
import com.CuiChangHe.studentmanager.entity.userinfo.UserInfoData;
import com.CuiChangHe.studentmanager.entity.userinfo.UserInfoReturnMsg;
import com.CuiChangHe.studentmanager.mapper.UserInfoMapper;
import com.CuiChangHe.studentmanager.service.userinfo.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description :用户信息业务实现
 * @Author :崔昌赫
 * @Date : 2021/11/16
 */
@Service
@Slf4j
public class UserInfoServiceImpl implements UserInfoService {

    private UserInfoMapper mapper;

    @Autowired
    public void setMapper(UserInfoMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    @Transactional(readOnly = true)
    public UserInfoReturnMsg getUserInfoByToken(String token) {
        UserInfoReturnMsg returnMsg = new UserInfoReturnMsg();
        log.info("即将开始查找用户信息.token:{}",token);
        if(token == null){
            returnMsg.setCode(50000);
            log.warn("获取到token为空!");
            returnMsg.setMessage("接收到token为空!");
            return returnMsg;
        }
        log.info("开始查找用户信息.token:{}",token);
        UserInfoDao userInfoByToken = mapper.getUserInfoByToken(token);
        log.info("查找到用户信息为:{}",userInfoByToken);
        if(userInfoByToken==null){
            returnMsg.setCode(50000);
            log.warn("查找用户信息为空!");
            returnMsg.setMessage("无法查找到用户信息!");
            return returnMsg;
        }
        returnMsg.setData(new UserInfoData(
                userInfoByToken.getRoles().split(","),
                userInfoByToken.getIntroduction(),
                userInfoByToken.getAvatar(),
                userInfoByToken.getName()));
        returnMsg.setCode(20000);
        return returnMsg;
    }
}
